package com.minivision.sdktestdemo.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.minivision.sdktestdemo.R;

/**
 * Created by devtang on 2016/5/21.
 */
public class ClearableEditText extends AppCompatEditText implements View.OnTouchListener, TextWatcher, View.OnFocusChangeListener {

    private Drawable mClearableDrawable;

    public ClearableEditText(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        if (getCompoundDrawables()[2] != null) {
            mClearableDrawable = getCompoundDrawables()[2];
        } else {
            mClearableDrawable = ContextCompat.getDrawable(context, R.drawable.lay_protype_btn_close);
        }
        mClearableDrawable.setBounds(0, 0, mClearableDrawable.getIntrinsicWidth(), mClearableDrawable.getIntrinsicHeight());
        setDrawableVisiable(false);
        setOnTouchListener(this);
        setOnFocusChangeListener(this);
        addTextChangedListener(this);
    }

    public void setDrawableVisiable(boolean flag) {
        mClearableDrawable.setVisible(flag, false);
        Drawable drawable = flag ? mClearableDrawable : null;
        setCompoundDrawables(getCompoundDrawables()[0], getCompoundDrawables()[1], drawable, getCompoundDrawables()[3]);
    }

    public ClearableEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ClearableEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mClearableDrawable.isVisible() &&
                event.getX() >= getWidth() - getPaddingRight() - mClearableDrawable.getIntrinsicWidth()
                && event.getX() <= getWidth() - getPaddingRight() && event.getY() <= getHeight() - getPaddingBottom()
                && event.getY() >= getHeight() - getPaddingBottom() - mClearableDrawable.getIntrinsicHeight()) {
            setText("");
            setDrawableVisiable(false);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
//            setDrawableVisiable(true);
            setDrawableVisiable(getText().length() > 0);
        } else {
            setDrawableVisiable(false);
        }
    }

    @Override
    public final void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
        if (isFocused()) {
            setDrawableVisiable(s.length() > 0);
        }
    }
}
